#!/bin/sh

PREFIX=/usr/local
OPT=yes
DBG=yes
VERSION=`head -n 1 README | sed 's/^.*- //'`

echo "configuring vmath $VERSION ..."

for arg; do
	case "$arg" in
	--prefix=*)
		value=`echo $arg | sed 's/--prefix=//'`
		PREFIX=${value:-$prefix}
		;;
	
	--enable-opt)
		OPT=yes;;
	--disable-opt)
		OPT=no;;

	--enable-debug)
		DBG=yes;;
	--disable-debug)
		DBG=no;;

	--help)
		echo 'usage: ./configure [options]'
		echo 'options:'
		echo '  --prefix=<path>: installation path (default: /usr/local)'
		echo '  --enable-opt: enable speed optimizations (default)'
		echo '  --disable-opt: disable speed optimizations'
		echo '  --enable-debug: include debugging symbols (default)'
		echo '  --disable-debug: do not include debugging symbols'
		echo 'all invalid options are silently ignored'
		exit 0
		;;
	esac
done

echo "prefix: $PREFIX"
echo "optimize for speed: $OPT"
echo "include debugging symbols: $DBG"

echo 'creating makefile ...'
echo "PREFIX = $PREFIX" >Makefile
if [ "$DBG" = 'yes' ]; then
	echo 'dbg = -g' >>Makefile
fi
if [ "$OPT" = 'yes' ]; then
	echo 'opt = -O3' >>Makefile
fi
if [ "`uname -s`" = Darwin ]; then
	echo 'shared = -dynamiclib' >>Makefile
	echo 'so = dylib' >>Makefile
else
	echo 'shared = -shared' >>Makefile
	echo 'so = so' >>Makefile
fi
cat Makefile.in >>Makefile

echo 'creating pkg-config file ...'
echo "prefix=$PREFIX" >vmath.pc
echo "ver=$VERSION" >>vmath.pc
cat vmath.pc.in >>vmath.pc

echo 'configuration completed, type make (or gmake) to build.'
